<?php

class Photomodel extends Model
{
	function Photomodel()
	{
		//Constructor
		parent::Model();
		
	}
	
	function getAlbum($userId, $orderBy = 'datetime desc', $start = '', $limit = '')
	{
		if ($userId == '') $userId = $this->session->userdata('userId');
		$this->db->select('*');
		$this->db->from('photos');
		$this->db->where('user_id', $userId);
		if (trim($orderBy) != '') $this->db->order_by($orderBy);
		if (trim($limit) != '' && is_numeric($limit))
			{
				if (trim($start) != '' && is_numeric($start)) $this->db->limit($limit, $start);
				else  $this->db->limit($limit);
			}
		$query = $this->db->get();
		
		$myPhoto = array();
		if($query->num_rows() > 0)
		{
			foreach($query->result_array() as $rowPhoto)
			{
				$myPhoto[] = $rowPhoto;
			}
			return $myPhoto;
		}
		else
			return false;
		
	}
	
	function getUserAlbum($userId, $orderBy = 'datetime desc', $start = '', $limit = '')
	{
		if ($userId == '') $userId = $this->session->userdata('userId');
		$this->db->select('*');
		$this->db->from('photos');
		$this->db->where('user_id', $userId);
		$this->db->where('photo_status', 'public');
		if (trim($orderBy) != '') $this->db->order_by($orderBy);
		if (trim($limit) != '' && is_numeric($limit))
			{
				if (trim($start) != '' && is_numeric($start)) $this->db->limit($limit, $start);
				else  $this->db->limit($limit);
			}
		$query = $this->db->get();
		
		$myPhoto = array();
		if($query->num_rows() > 0)
		{
			foreach($query->result_array() as $rowPhoto)
			{
				$myPhoto[] = $rowPhoto;
			}
			return $myPhoto;
		}
		else
			return false;
		
	}
	
	function getPhoto($photoId)
	{
		if ($photoId == '') $photoId = $this->uri->segment(3);
		$this->db->select('*');
		$this->db->from('photos');
		$this->db->where('photo_id', $photoId);
		$query = $this->db->get();
		$photo = $query->result_array();
		return $photo;
	}
	
	function getUserPhoto($photoId)
	{
		if ($photoId == '') $photoId = $this->uri->segment(3);
		$this->db->select('*');
		$this->db->from('photos');
		$this->db->where('photo_id', $photoId);
		$this->db->where('photo_status', 'public');
		$query = $this->db->get();
		$photo = $query->result_array();
		return $photo;
	}
	
	function getGeo($lat, $lng)
	{

		$url = 'http://maps.google.com/maps/geo?q='.$lat.','.$lng.'&output=json&ie=utf8&oe=utf8&sensor=false&key=ABQIAAAA1QnAxBtPWpvkkIJ1M7uNRhSaZr1EoDksNVh5CWAzOfD4zrQihBTtbZTHEHq4q97m9-r8YVi8kTiliA';
		$json = file_get_contents($url);
		$arr = json_decode($json);
	  @$address = $arr->Placemark[0]->address;
		
		if($address != '')
		{
			return $address;
		}
		else
		{
			return false;
		}
	}
	
	function addPoint($lat, $lng, $photoId)
	{
		$this->db->where('photo_id', $photoId);
		$this->db->update('photos', array('latitude' => $lat, 'longitude' => $lng));

		$this->session->set_flashdata('success',  'ระบุตำแหน่งเรียบร้อยแล้ว.');
		redirect('profile/addPoint/'.$photoId);
	}
	
	function delete($photoId)
	{
		$userId = $this->session->userdata('userId');
		
		$this->db->select('caption');
		$this->db->from('photos');
		$this->db->where('photo_id', $photoId);
		$query = $this->db->get();
		foreach($query -> result() as $row)
		{
			$file = $row->caption;
		}
		$paththumb = './application/userdata/profile_' . $userId . '/photos/thumbs/' . $file;
		unlink($paththumb);
		$path = './application/userdata/profile_' . $userId . '/photos/' . $file;
		unlink($path);
		
		//
		$this->db->where('photo_id', $photoId);
		$this->db->delete('photos');
		
		$this->session->set_flashdata('success',  'ลบรูปภาพเรียบร้อยแล้ว.');
		redirect('profile/album');
	}
	
	function getLastPhoto($orderBy = 'datetime desc', $start = '', $limit = '')
	{
		$this->db->select('*');
		$this->db->from('photos');
		$this->db->where('photo_status', 'public');
		if (trim($orderBy) != '') $this->db->order_by($orderBy);
		if (trim($limit) != '' && is_numeric($limit))
			{
				if (trim($start) != '' && is_numeric($start)) $this->db->limit($limit, $start);
				else  $this->db->limit($limit);
			}
		$query = $this->db->get();
		
		$myPhoto = array();
		if($query->num_rows() > 0)
		{
			foreach($query->result_array() as $rowPhoto)
			{
				$myPhoto[] = $rowPhoto;
			}
			return $myPhoto;
		}
		else
			return false;
		
	}
	
	function getPopular($orderBy = 'votes_up desc', $start = '', $limit = '')
	{
		$this->db->select('*');
		$this->db->from('photos');
		$this->db->where('votes_up !=', 0);
		$this->db->where('votes_down <=', 1);
		$this->db->where('photo_status', 'public');
		if (trim($orderBy) != '') $this->db->order_by($orderBy);
		if (trim($limit) != '' && is_numeric($limit))
			{
				if (trim($start) != '' && is_numeric($start)) $this->db->limit($limit, $start);
				else  $this->db->limit($limit);
			}
		$query = $this->db->get();
		
		$myPhoto = array();
		if($query->num_rows() > 0)
		{
			foreach($query->result_array() as $rowPhoto)
			{
				$myPhoto[] = $rowPhoto;
			}
			return $myPhoto;
		}
		else
			return false;
		
	}
	
	function getPhotoType($type, $orderBy = 'datetime desc', $start = '', $limit = '')
	{
		$this->db->select('*');
		$this->db->from('photos');
		$this->db->where('photo_status', 'public');
		$this->db->where('type', $type);
		if (trim($orderBy) != '') $this->db->order_by($orderBy);
		if (trim($limit) != '' && is_numeric($limit))
			{
				if (trim($start) != '' && is_numeric($start)) $this->db->limit($limit, $start);
				else  $this->db->limit($limit);
			}
		$query = $this->db->get();
		
		$myPhoto = array();
		if($query->num_rows() > 0)
		{
			foreach($query->result_array() as $rowPhoto)
			{
				$myPhoto[] = $rowPhoto;
			}
			return $myPhoto;
		}
		else
			return false;
		
	}
	
}
